<?php
declare (strict_types = 1);

namespace app\command;

use OpenApi\Generator;
use think\App;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Swagger extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('exec')
            ->addArgument('dir',Argument::OPTIONAL,"扫描的目录")
            ->setDescription('swagger yaml 生成');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $dir = $input->getArgument('dir');
        $appPath = App::getInstance()->getAppPath();
        $rootPath = App::getInstance()->getRootPath();
        $swaggerPath = $rootPath.'public'.DIRECTORY_SEPARATOR.'swagger';
        if(!is_dir($swaggerPath)){
          mkdir($swaggerPath,0777);
        }
        $allPath = $appPath.$dir;
        $openapi = Generator::scan([$allPath]);
        header('Content-Type: application/x-yaml');
        $content =  $openapi->toYaml();
        $number = file_put_contents($swaggerPath.DIRECTORY_SEPARATOR.$dir.'.yaml',$content);
        $number ? $output->writeln('创建成功') : $output->writeln('创建失败');
        $output->writeln("创建完成");
    }
}
